package com.cheetah.start.system.version.controller;

import java.util.HashMap;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cheetah.start.common.base.result.Result;
import com.cheetah.start.common.base.result.ResultController;
import com.cheetah.start.common.base.result.ResultTable;
import com.cheetah.start.common.query.PageQuery;
import com.cheetah.start.system.version.domain.SysVersion;
import com.cheetah.start.system.version.service.SysVersionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xjhmwj
 * @date 2021/7/9 15:39
 */
@Slf4j
@RestController
@RequestMapping("/version")
public class SysVersionController extends ResultController {
    @Autowired
    private SysVersionService sysVersionService;


    /**
     * 获取最新版本信息
     *
     * @return {@link Result}
     */
    @GetMapping("/get")
    public Result<?> get() {
        HashMap<String, String> res = new HashMap<>();
        res.put("version", "v1.1.0");
        res.put("info", "增加文件管理");
        res.put("time", "2021-08-07 22:08:07");
        SysVersion version = new SysVersion();
        version.setVersion(res.get("version"));
        version.setInfo(res.get("info"));
        version.setReleaseTime(res.get("time"));
        QueryWrapper<SysVersion> wrapper = new QueryWrapper<>();
        wrapper.eq("version", version.getVersion());
        SysVersion sysVersion = sysVersionService.getOne(wrapper);
        if (sysVersion == null) {
            boolean save = sysVersionService.save(version);
            if (!save) {
                log.warn("版本信息数据库插入失败");
            }
            res.put("update", "true");
        } else {
            res.put("update", "false");
        }
        return success(res);
    }

    /**
     * 列表
     *
     * @param pageQuery 页面查询
     * @return {@link ResultTable}
     */
    @GetMapping("/list")
    public ResultTable list(PageQuery pageQuery) {
        Page<SysVersion> page = new Page<>(pageQuery.getPage(), pageQuery.getLimit());
        QueryWrapper<SysVersion> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        sysVersionService.page(page, wrapper);
        return pageTable(page.getRecords(), page.getTotal());
    }

}
